package com.dzpk.gameserver.net;

import com.dzpk.gameserver.action.CommandEnum;
import com.dzpk.gameserver.module.player.Player;
import com.dzpk.gameserver.module.player.PlayerControler;
import com.dzpk.gameserver.util.BeanControler;
import com.dzpk.gameserver.util.CommonPackageUtil;
import com.dzpk.gameserver.util.GameServerConstants;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
import somnus.net.DefaultHandler;
import somnus.net.message.codec.MessageCodecFactory;
import somnus.task.TaskAdapter;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public class GameServerHandler extends DefaultHandler {

    public GameServerHandler(String name) {
        super(name);
    }

    public GameServerHandler(String name, boolean support) {
        super(name, support);
    }

    private static Set<Integer> actionMap;

    private int maxSession;

    private AtomicInteger robotCount = new AtomicInteger(0);

    static {
        actionMap = new HashSet<Integer>();
        for (CommandEnum a : CommandEnum.values()) {
            if (a.getDesc().startsWith("C2S_")) {
                actionMap.add(a.getId());
            }
        }
    }

    @Override
    public boolean isValidMessageId(int messageId) {
        return actionMap.contains(messageId);
    }

    public int getMaxSession() {
        return maxSession;
    }

    public void setMaxSession(int maxSession) {
        this.maxSession = maxSession;
    }

    @Override
    public void connectionIdle(IoSession session, IdleStatus status) {
//        Player player = (Player) session.getAttribute(GameServerConstants.SIGN_SESSION_PLAYER);
//        if (player == null) {
//            return;
//        }
//
//        if (player.isActive()) {
//            player.setActive(false);
//            session.setAttribute("idle", System.currentTimeMillis());
//            player.sendMessage(CommandEnum.S2C_PING_ALIVE);
//        } else {
//            try {
//                Player onlinePlayer = PlayerControler.getInstance().getPlayerByUserId(player.getPlayerId());
//                if (onlinePlayer == null || onlinePlayer == player) {
//                    log.info(String.format("robot kick offline, user : %s, cost : %ss", player.getUserId(), (System
//                            .currentTimeMillis() - (Long) session.getAttribute("idle")) / 1000));
//                    // 设置总在线时长
////                    player.getDayCounter().set(DayCountEnum.TIMER_GIFT_ALL_TIME,
////                            (GameServerUtils.getCurrentTime() - player.loginTempTime));
//                    player.offline(GameServerConstants.PLAYER_OFFLINE_NETWORK_OUT);
//                } else {
//                    session.removeAttribute(GameServerConstants.SIGN_SESSION_PLAYER);
//                }
//            } finally {
//                session.close(true);
//                log.info(String.format("detect zombie session,total=%s", robotCount.incrementAndGet()));
//            }
//        }
    }

    @Override
    public void afterSessionClosed(IoSession session) {
        log.info(String.format("session closed,sid=%s", session.getId()));
        final Player player = (Player) session.getAttribute(GameServerConstants.SIGN_SESSION_PLAYER);

    }

    @Override
    public void afterSessionOpened(IoSession session) {
        AbstractSocketSessionConfig s = (AbstractSocketSessionConfig) session.getConfig();
        if (!s.isTcpNoDelay()) {
            s.setTcpNoDelay(true);
        }
        Player player = Player.createPlayer(session);
        session.setAttribute(GameServerConstants.SIGN_SESSION_PLAYER, player);
        if (player != null) {
//            player.getSession().write(
//                    CommonPackageUtil.getDataPackage(CommandEnum.S2C_SESSION_CREATED.getId(), StructString
//                            .newBuilder().setValue(MessageCodecFactory.SESSION_ENCRYPT).build().toByteArray()));
        }
    }


}
